<?php
namespace app\addons\controller;
use think\Controller;
use think\Db;
use \think\Request;
// use app\index\
class Index extends Base
{
    public function index()
    {
        $cid=request()->param('cid');
        // print_r($cid);
        $article=db("plugin");
        // $category=db("plugin_category");
        $keywords=request()->param('keywords');
        // $fragwh['status']=1;
        // $fragwh['type']=1;
        // $cate=$category->where($fragwh)->select();
       
        // $where['status']=1;
        if($cid){
            $where['cid']=$cid;
        }

        if($keywords){
            $where['name']=['like', '%'.$keywords.'%'];
        }

        $count=$article->where($where)->count();
        $data=$article->where($where)->order("sort asc,id desc")->select();
        // for ($i=0; $i < count($data); $i++) { 
        //     $data[$i]['CName']=$category->where("id",$data[$i]['cid'])->value("name");       
        //     if(!$data[$i]['CName']){
        //         $data[$i]['CName']='无分组';
        //     }
        // }

        $this->assign("cid",$cid);
        // $this->assign("category",$cate);
        $this->assign("mess",$data);
        $this->assign("count",$count);
        return view('index');
    }
    public function index_del(){
        $id=$_GET['id'];
        $res=db("plugin")->delete($id);
        if($res){
            $mess['code']=1;
            $mess['msg']="删除成功";
        }else{
            $mess['code']=0;
            $mess['msg']="删除失败";
        }       // print_r($data);exit;
        return json_encode($mess);
    }
    public function addArticle()
    {
        $id=request()->param('id');
        if($id){
            $mess=db("plugin")->where("id",$id)->find();
            $this->assign("mess",$mess);
        }        
        // $where['status']=1;   
        // $where['type']=1;
        // $category=db("plugin_category")->where($where)->select();
        
        // $this->assign("category",$category);
        return view('addArticle');
    }
    public function aboutAddDone()
    {
        $updid=request()->param('updid'); 
        $data=$_POST;
        $data['aid']=$this->userMsg['id'];
        if($updid){
            $res=db("plugin")->where("id",$updid)->update($data);
        }else{
            $res=db("plugin")->insert($data);
        }
        if($res){
            $mess['code']=200;
            $mess['msg']='保存成功';
        }else{
            $mess['code']=100;
            $mess['msg']='保存失败';
        }        
        return json_encode($mess);
    }
    public function category()
    {
        return view('category');
    }    
    public function getCate()
    {
        $category=db("plugin_category");
        $where['status']=1;
        $where['type']=1;
        $count=$category->where($where)->count();
        $data=$category->where($where)->order("pid asc , sort desc,id asc")->select();

        $mess['code']=0;
        $mess['msg']='';
        $mess['count']=$count;
        $mess['data']=$data;
        return json_encode($mess);
    }
    public function addCate()
    {
        $id=request()->param('id');        
        // print_r($id);exit;
        $where['type']=1;
        $where['status']=1;
        $where['pid']=0;          
        $category=db("plugin_category")->where($where)->select();
        if($id){
            $mess=db("plugin_category")->where("id",$id)->find();
            $this->assign("mess",$mess);
        }
        $this->assign("category",$category);
        return view('addCate');
    }
    public function addCateDone()
    {
        $updid=request()->param('updid'); 
        $data=$_POST;
        $data['type']=1;
        $data['aid']=$this->userMsg['id'];
        if($updid){
            $res=db("plugin_category")->where("id",$updid)->update($data);
        }else{
            $res=db("plugin_category")->insert($data);
        }        

        if($res){
            $mess['code']=200;
            $mess['msg']='添加成功';
        }else{
            $mess['code']=100;
            $mess['msg']='添加失败';
        }        
        return json_encode($mess);
    }
    public function cate_del(){
        $id=$_GET['id'];
        $article=db("plugin")->where("cid=".$id)->select();
        if($article){
            $mess['code']=0;
            $mess['msg']="该分组下有内容，请先处理！"; 
        }else{
            $res=db("plugin_category")->delete($id);
            if($res){
                $mess['code']=1;
                $mess['msg']="删除成功";
            }else{
                $mess['code']=0;
                $mess['msg']="删除失败";
            }       // print_r($data);exit;
        }        
        return json_encode($mess);
    }
    public function indexSort(){
        $id=request()->param('id');            
        $sort=request()->param('sort');

        $data['sort']=$sort;
        $res=db("plugin")->where("id",$id)->update($data);
        if($res){
            $mess['code']=1;
            $mess['msg']="操作成功";
        }else{
            $mess['code']=0;
            $mess['msg']="操作失败";
        }
        return json_encode($mess);   
    }
    public function changeStatus(){
        $id=request()->param('id');            
        $status=request()->param('status');
        // return json_encode($_GET); 
        // $data=db("plugin")->where("status = 1 and pid=".$id)->select();
        // if($data && $status == 0){
        //     $mess['code']=0;
        //     $mess['msg']="停用失败，该栏目下有已用子栏目";
        //     return json_encode($mess);
        // }
        $data['status']=$status;
        $res=db("plugin")->where("id",$id)->update($data);
        if($res){
            $mess['code']=1;
            $mess['msg']="操作成功";
        }else{
            $mess['code']=0;
            $mess['msg']="操作失败";
        }
        return json_encode($mess);   
    }
}
